/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import Protocol.PacketFactory;
import Protocol.PacketTypeKeeper;
import Protocol.QueryRequestPacket;
import Protocol.QueryResponsePacket;
import config.Content;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * Dialog to add a new query
 * @author Kay van Bree
 */
public class NewQueryDialog extends JDialog implements ActionListener {
	private JLabel labelName, labelQuery;
	private JTextField textName;
	private JTextArea textQuery;
	private JButton buttonSave;
	private JButton buttonCancel;
	private JLabel labelTitle;
	
	public NewQueryDialog(){
		setSize(250,200);
		setLocationRelativeTo(null);
		setLayout(new FlowLayout());
		setTitle("New Query");
		setResizable(false);
		buildForm();
		
		setVisible(true);
	}
	
	/**
	 * Build the form
	 * @author Kay van Bree
	 */
	private void buildForm(){
		labelName = new JLabel("Name:");
		add(labelName);
		textName = new JTextField(15);
		textName.setBorder(BorderFactory.createEtchedBorder());
		add(textName);
		
		labelQuery = new JLabel("Query:");
		add(labelQuery);
		textQuery = new JTextArea(6, 15);
		textQuery.setLineWrap(true);
		textQuery.setBorder(BorderFactory.createEtchedBorder());
		add(textQuery);
		
		buttonSave = new JButton("Save");
		buttonSave.addActionListener(this);
		add(buttonSave);
		
		buttonCancel = new JButton("Cancel");
		buttonCancel.addActionListener(this);
		add(buttonCancel);
	}
	
	/**
	 * Handles the actions
	 * @author Kay van Bree
	 * @param e 
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == buttonSave) performSave();
		if(e.getSource() == buttonCancel) performCancel();
	}
	
	/**
	 * Sends a query request packet to the server, saving this query
	 * @author Kay van Bree
	 */
	public void performSave(){
		String name = textName.getText();
		String query = textQuery.getText();
		
		if(check(query)){
			try {
				QueryRequestPacket request = (QueryRequestPacket) PacketFactory.getPacket(PacketTypeKeeper.QUERYREQUESTPACKET);
                                request.setStatus(QueryRequestPacket.INSERT);
				request.setQuery(query);
                                request.setQueryName(name);
				QueryResponsePacket response = (QueryResponsePacket) Content.connection.communicate(request);
				if(response.getQueryResult() == QueryResponsePacket.QUERYSUCCES){
					Content.log.println("Query succesfully added!");
					setVisible(false);
				} else {
					Content.log.println("Query has not been saved.");
				}
			} catch (Exception ex) {
				Content.log.println("Error while saving query: " + ex.getMessage());
			}
		}
	}
	
	/**
	 * Checks if the query is executable
	 * @author Kay van Bree
	 * @param query
	 * @return True if the query is valid
	 */
	public boolean check(String query){
		try {
			QueryRequestPacket request = new QueryRequestPacket(1);
			request.setQuery(query);
			QueryResponsePacket response = (QueryResponsePacket) Content.connection.communicate(request);
			if(response.getQueryResult() != QueryResponsePacket.QUERYSUCCES){
				throw new Exception();
			}
		} catch (Exception ex) {
			Content.log.println("Given query could not be processed, check your query");
			return false;
		}
		Content.log.println("Given query has been validated...");
		return true;
	}
	
	/**
	 * Cancels this dialog
	 * @author Kay van Bree
	 */
	public void performCancel(){
		setVisible(false);
	}	
}
